package flex.cc.common.utils;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import flex.cc.PublicUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * @author wkangle
 * @className SendSmsUtil
 * @description 发送短信
 * @time 2021-10-21 15:35:20
 */
@Component
@Slf4j
@PropertySource("classpath:application.yml")
public class SendSmsUtil {

    @Value("${sms.SecretId}")
    private String SecretId;

    @Value("${sms.SecretKey}")
    private String SecretKey;

    @Value("${sms.endpoint}")
    private String endpoint;

    @Value("${sms.region}")
    private String region;

    @Value("${sms.sms-sdk-appId}")
    private String smsSDKAppId;

    @Value("${sms.sign-name}")
    private String signName;

    @Value("${sms.template.username}")
    private String username;

    @Value("${sms.template.order-no}")
    private String orderNo;

    @Value("${sms.template.end-order-no}")
    private String endOrderNo;

    @Value("${sms.template.service-item}")
    private String serviceItem;

    @Value("${sms.template.code}")
    private String code;

    @Value("${sms.template.time}")
    private String time;

    private static final Logger logger = LoggerFactory.getLogger(SendSmsUtil.class);

    /**
     * @description 发送短信
     * @param templateId 模版id
     * @param phoneNumberSet 手机号列表
     * @param templateParamSet 模板所需参数列表
     * @return
     * @author wkangle
     * @time 2021/10/22 0022 16:23
     * @exception/throws
    */
    public String send(String templateId, String[] phoneNumberSet, String[] templateParamSet) {
        String result = null;
        try {
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            Credential cred = new Credential(SecretId, SecretKey);

            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(endpoint);

            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            // 实例化要请求产品的client对象,clientProfile是可选的
            SmsClient client = new SmsClient(cred, region, clientProfile);

            // 实例化一个请求对象,每个接口都会对应一个request对象
            SendSmsRequest req = new SendSmsRequest();
            // 手机号
            req.setPhoneNumberSet(phoneNumberSet);
            // sdkAppId
            req.setSmsSdkAppId(smsSDKAppId);
            // 签名
            req.setSignName(signName);
            // 模版id
            req.setTemplateId(templateId);
            // 模板参数
            if (PublicUtil.isNotEmpty(templateParamSet)) {
                req.setTemplateParamSet(templateParamSet);
            }

            // 请求参数
            String requestBody = SendSmsRequest.toJsonString(req);

            // 返回的resp是一个SendSmsResponse的实例，与请求对象对应
            SendSmsResponse resp = client.SendSms(req);

            // 输出json格式的字符串回包
            result = SendSmsResponse.toJsonString(resp);
            logger.info(requestBody);
            logger.info("腾讯云发送短信 ==>> \nBody:{}, \nResult:{}", requestBody, result);
        } catch (TencentCloudSDKException e) {
            logger.info(e.toString());
            e.getStackTrace();
        } catch (Exception e) {
            logger.info(e.toString());
            e.getStackTrace();
        }
        return result;
    }

    /**
     * @description 获取短信参数列表
     * @param value2 参数1
     * @param value3 参数2
     * @param value4 参数3
     * @param value5 参数4
     * @param username 客户名称
     * @param orderNo 救援单号
     * @param serviceItem 服务类型
     * @param time 时长
     * @author wkangle
     * @time 2021/10/28 0028 9:09
     * @exception/throws
     */
    public String[] getTemplateParamSet(String value2, String value3, String value4, String value5,
                                         String username, String orderNo, String serviceItem, String code, String time) {
        String param2 = this.shiftSmsParams(value2, username, orderNo, serviceItem, code, time);
        String param3 = this.shiftSmsParams(value3, username, orderNo, serviceItem, code, time);
        String param4 = this.shiftSmsParams(value4, username, orderNo, serviceItem, code, time);
        String param5 = this.shiftSmsParams(value5, username, orderNo, serviceItem, code, time);
        String[] templateParamSet = null;
        if (PublicUtil.isNotEmpty(param5)) {
            templateParamSet = new String[] {param2, param3, param4, param5};
        } else if (PublicUtil.isNotEmpty(param4)) {
            templateParamSet = new String[] {param2, param3, param4};
        } else if (PublicUtil.isNotEmpty(param3)) {
            templateParamSet = new String[] {param2, param3};
        } else if (PublicUtil.isNotEmpty(param2)) {
            templateParamSet = new String[]{param2};
        }
        return templateParamSet;
    }

    /**
     * @description 根据字典表中短信模版的value 转成对应的实参
     * @param value 字典表中短信模版中的value
     * @param username 客户名称
     * @param orderNo 救援单号
     * @param serviceItem 服务类型
     * @param time 时长
     * @return
     * @author wkangle
     * @time 2021/10/28 0028 9:08
     * @exception/throws
     */
    private String shiftSmsParams(String value, String username, String orderNo, String serviceItem, String code, String time){
        if (PublicUtil.isEmpty(value)) {
            return null;
        }
        if (this.username.equals(value)) {
            return username;
        }
        if (this.orderNo.equals(value)) {
            return orderNo;
        }
        if (this.endOrderNo.equals(value)) {
            return orderNo.substring(10, 14);
        }
        if (this.serviceItem.equals(value)) {
            return serviceItem;
        }
        if (this.code.equals(value)) {
            return code;
        }
        if (this.time.equals(value)) {
            return time;
        }
        return null;
    }


}
